package com.feiyu.sys.web

import com.alibaba.fastjson.JSONObject
import com.feiyu.sys.utils.DynamicSqlExecuteUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

/**
 * Created by zdy on 2017/11/28.
 */
@RestController
@RequestMapping(value = "/dynamicSqlExecute")
class DynamicSqlExecuteController {

    @Autowired
    DynamicSqlExecuteUtils executeUtils
    /**
     * 执行sql语句 获取多行数据
     * @param sql
     * @return
     */
    @RequestMapping(value = "/getMultiColumns", method = RequestMethod.POST)
    ResponseVo getMultiColumns(@RequestBody String sql){
        ResponseVo result = new ResponseVo()
        if(null == sql){
            result.success = false
            result.msg = "sql不能为null!"
            return result
        }
        try{
            JSONObject object = JSONObject.parse(sql)
            result = executeUtils.getMultiColumns(object.get("sql"))
        } catch (Exception e) {
            result.success = false
            result.msg = e
        }
        return result
    }
    /**
     * 执行sql语句 获取一行数据
     * @param sql
     * @return
     */
    @RequestMapping(value = "/getOneColumn", method = RequestMethod.POST)
    ResponseVo getOneColumn(@RequestBody String sql){
        ResponseVo result = new ResponseVo()
        if(null == sql){
            result.success = false
            result.msg = "sql不能为null!"
            return result
        }
        try{
            JSONObject object = JSONObject.parse(sql)
            result = executeUtils.getOneColumn(object.get("sql"))
        } catch (Exception e) {
            result.success = false
            result.msg = e
        }
        return result
    }
}





class ResponseVo{
    // 成功标记
    boolean success = true
    //条目数
    Long count
    // 错误信息
    String msg = ""
    // 返回的具体数据
    Object data
}